package project;
import java.nio.charset.Charset;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import logtest.LogBack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: sun
 * @Description: 简单测试布隆过滤器
 * @Data: create in 13:18 2018/12/10
 * @Modfied by:
 */

public class Bloom {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    public BloomFilter<String> bloomFilter = BloomFilter.create(Funnels.stringFunnel(Charset.forName("utf-8")), 3000000, 0.000001);

    public Boolean put(String date){
        try {
            this.bloomFilter.put(date);
            return true;
        }catch (Exception e){
            logger.error("bloom filter put error: "+e.toString());
            return false;
        }
    }

    public static void main(String[] args) {

        BloomFilter<String> b = BloomFilter.create(Funnels.stringFunnel(Charset.forName("utf-8")), 1000, 0.000001);
        System.out.println(b.hashCode());
        System.out.println(b.approximateElementCount());
        b.put("wx4g0ec1");
        System.out.println(b.hashCode());
        System.out.println(b.approximateElementCount());
        b.put("wx4g0ec4");
        System.out.println(b.hashCode());
        System.out.println(b.approximateElementCount());
        b.put("wx4g0ec6");
        System.out.println(b.hashCode());
        System.out.println(b.approximateElementCount());


        BloomFilter<String> c = BloomFilter.create(Funnels.stringFunnel(Charset.forName("utf-8")), 1000, 0.000001);
        System.out.println(c.hashCode());
        System.out.println(c.approximateElementCount());
        c.put("wx4g0ec1");
        System.out.println(c.hashCode());
        System.out.println(c.approximateElementCount());
        c.put("wx4g0ec4");
        System.out.println(c.hashCode());
        System.out.println(c.approximateElementCount());
        c.put("wx4g0ec6");
        System.out.println(c.hashCode());
        System.out.println(c.approximateElementCount());

        System.out.println(b.mightContain("wx4g0ec6"));


        BloomFilter<String> b1 = BloomFilter.create(Funnels.stringFunnel(Charset.forName("utf-8")), 1000, 0.000001);
        b1.put("aba");
        b1.put("abb");
        b1.put("abc");
        b1.putAll(b);
        System.out.println(b1.mightContain("wx4g0ec4"));
    }

}
